import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class test {
    static class MyStack {
        public Queue<Integer> q1;
        public Queue<Integer> q2;
        public MyStack() {
            q1=new LinkedList<>();
            q2=new LinkedList<>();
        }

        public void push(int x) {
            if(!q1.isEmpty()){
                q1.offer(x);
            }else{
                q2.offer(x);
            }
        }

        public int pop() {
            if(empty()){
                return -1;
            }
            if(q1.isEmpty()){
                int size=q2.size();
                for(int i=0;i<size-1;i++){
                    q1.offer(q2.poll());

                }
                return q2.poll();
            }else{
                int size=q1.size();
                for(int i=0;i<size-1;i++){
                    q2.offer(q1.poll());

                }
                return q1.poll();
            }

        }
        public int val=0;
        public int top() {
            if(empty()){
                return -1;
            }
            if(q1.isEmpty()){
                int size = q2.size();
                for(int i=0;i<size;i++){
                     val=q2.peek();
                    q1.offer(q2.poll());

                }
                return val;
            }else{
                int size=q1.size();
                for(int i=0;i<size;i++){
                    val=q1.peek();
                    q2.offer(q1.poll());

                }
                return val;
            }
        }

        public boolean empty() {
            return q1.isEmpty()&&q2.isEmpty();
        }

        public static void main1(String[] args) {
            MyStack s=new MyStack();
            s.push(1);
            s.push(2);
            System.out.println(s.top());
            System.out.println(s.pop());
        }
        static class MyQueue {
            public Stack<Integer> s1;
            public Stack<Integer> s2;
            public MyQueue() {
                s1=new Stack<>();
                s2=new Stack<>();
            }

            public void push(int x) {
                s1.push(x);
            }

            public int pop() {
                if(empty()){
                    return -1;
                }
                if(!s1.isEmpty()){
                    int size=s1.size();
                    for(int i=0;i<size;i++){
                        s2.push(s1.pop());
                    }
                }
                return s2.pop();
            }

            public int peek() {
                if(empty()){
                    return -1;
                }
                if(!s1.isEmpty()){
                    int size=s1.size();
                    for(int i=0;i<size;i++){
                        s2.push(s1.pop());
                    }
                }
                return s2.peek();
            }

            public boolean empty() {
                return s1.isEmpty()&&s2.isEmpty();
            }
        }

        public static void main(String[] args) {
            MyQueue q=new MyQueue();
            q.push(1);
            System.out.println(q.pop());
        }

    }

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */
}
